package com.vne.skyemart.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.vne.skyemart.bean.UserSummaryBean;
import com.vne.skyemart.dao.UserDao;
import com.vne.skyemart.model.ADUser;
import com.vne.skyemart.service.UserService;
import com.vne.skyemart.util.CommonUtils;

@Service("userService")
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class UserServiceImpl implements UserService{
	private static final Logger logger = Logger.getLogger(UserServiceImpl.class);
	@Autowired
	private UserDao userDao;

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	public UserServiceImpl() {}
	@Transactional(propagation = Propagation.SUPPORTS,readOnly = false)
	@Override
	public void addUser(ADUser user) {
		userDao.save(user);
	}

	@Override
	public List<ADUser> listUser() {
		return userDao.findAll();
	}

	public ADUser findUserByUsername(String username, String isActive, String isDelete) {
		return userDao.findUserByUsername(username, isActive, isDelete);
	}

	@Override
	public List<UserSummaryBean> getAllUserSummary() {
		List<UserSummaryBean> beanList = null;
		try {
			List<ADUser> userList = userDao.findAll();
			if(userList != null && userList.size() > 0){
				beanList = new ArrayList<UserSummaryBean>();
				for (ADUser user : userList) {
					UserSummaryBean bean = new UserSummaryBean();
					bean.setUserId(String.valueOf(user.getUserID()));
					bean.setUserType(user.getUserType().getDecription());
					bean.setUserName(user.getUsername());
					bean.setFullName(user.getFullName());
					bean.setGender(String.valueOf(user.getGender()));
					bean.setEmail(user.getEmail());
					bean.setPhone(user.getPhone());
					bean.setIsOnline(CommonUtils.isUserOnline(user.getUsername())?"Online":"Offline");
					bean.setCreatedDate(user.getCreatedDate());
					bean.setIsActived(user.getIsActived());
					bean.setIsDeleted(user.getIsDeleted());
					beanList.add(bean);
				}
			}
			return beanList;
		} catch (Exception e) {
			logger.error("--Can not get user summary--");
			return null;
		}
	}
	
}
